from pydantic import BaseModel, Field
from typing import Optional, Dict, Any, List
from datetime import datetime

class WingknifeProcessBase(BaseModel):
    """影刀流程基础模型"""
    name: str = Field(..., description="流程名称", example="数据采集流程")
    description: Optional[str] = Field(None, description="流程描述", example="自动从网站采集数据的影刀流程")
    parameters: Dict[str, Any] = Field(..., description="流程配置参数", example={"targetUrl": "https://example.com", "dataFields": ["title", "price", "stock"]})

class WingknifeProcessCreate(WingknifeProcessBase):
    """创建影刀流程的请求模型"""
    process_define_id: str = Field(..., description="流程定义ID")
    output_file_path: str = Field(..., description="输出目录路径")
    # created_at: datetime = Field(..., description="创建时间")
    # updated_at: datetime = Field(..., description="更新时间")
    created_by: Optional[str] = Field(None, description="创建用户")
    pass

class WingknifeProcessUpdate(BaseModel):
    """更新影刀流程的请求模型"""
    name: Optional[str] = Field(None, description="流程名称", example="更新后的数据采集流程")
    process_define_id: Optional[str] = Field(None, description="流程定义ID")
    description: Optional[str] = Field(None, description="流程描述", example="更新后的流程说明")
    parameters: Optional[Dict[str, Any]] = Field(None, description="流程配置参数", example={"targetUrl": "https://updated-example.com", "dataFields": ["title", "price", "stock", "description"]})
    output_file_path: Optional[str] = Field(None, description="输出目录路径", example="D:/影刀RPA/参数文件/wingknife_params.json")

class WingknifeProcess(WingknifeProcessBase):
    """影刀流程返回模型"""
    id: int = Field(..., description="流程ID")
    process_define_id: str = Field(..., description="流程定义ID")
    output_file_path: str = Field(..., description="输出目录路径")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    created_by: str = Field(..., description="创建用户")

    class Config:
        from_attributes = True
        json_schema_extra = {
            "example": {
                "id": 1,
                "name": "数据采集流程",
                "description": "自动从网站采集数据的影刀流程",
                "parameters": {"targetUrl": "https://example.com", "dataFields": ["title", "price", "stock"]},
                "output_file_path": "uploads/wingknife/output/20230101121212",
                "created_at": "2023-01-01T12:12:12",
                "updated_at": "2023-01-01T12:12:12",
                "created_by": "admin"
            }
        }

class WingknifeExecutionBase(BaseModel):
    """影刀流程执行记录基础模型"""
    process_id: int = Field(..., description="流程ID", example=1)
    status: str = Field(..., description="状态：pending/running/completed/failed", example="running")
    parameters: Dict[str, Any] = Field(default_factory=dict, description="执行参数", example={"date": "2023-01-01", "limit": 100})

class WingknifeExecutionUpdate(BaseModel):
    """更新影刀流程执行记录的请求模型"""
    status: Optional[str] = Field(None, description="状态", example="completed")
    result: Optional[Dict[str, Any]] = Field(None, description="执行结果", example={"recordsProcessed": 120, "successCount": 118, "failureCount": 2})
    completed_at: Optional[datetime] = Field(None, description="完成时间")

class WingknifeExecution(BaseModel):
    """影刀流程执行记录返回模型"""
    id: int = Field(..., description="执行记录ID")
    process_id: int = Field(..., description="流程ID", example=1)
    run_id: str = Field(..., description="运行ID")
    parameters: Dict[str, Any] = Field(default_factory=dict, description="执行参数", example={"date": "2023-01-01", "limit": 100})
    status: str = Field(..., description="状态：pending/running/completed/failed", example="running")
    result: Optional[Dict[str, Any]] = Field(None, description="执行结果")
    started_at: datetime = Field(..., description="开始时间")
    completed_at: Optional[datetime] = Field(None, description="完成时间")
    created_at: Optional[datetime] = Field(None, description="创建时间")
    created_by: Optional[str] = Field(None, description="创建用户")
    process: Optional[WingknifeProcess] = None

    class Config:
        from_attributes = True
        json_schema_extra = {
            "example": {
                "id": 1,
                "process_id": 1,
                "run_id": "abc-123-xyz",
                "status": "completed",
                "parameters": {"date": "2023-01-01", "limit": 100},
                "result": {"recordsProcessed": 120, "successCount": 118, "failureCount": 2},
                "started_at": "2023-01-01T12:12:12",
                "completed_at": "2023-01-01T12:35:46",
                "created_at": "2023-01-01T12:12:12",
                "created_by": "admin"
            }
        }

class PaginatedResult(BaseModel):
    """分页结果基础模型"""
    total: int = Field(..., description="总记录数", example=100)
    page: int = Field(..., description="当前页码", example=1)
    page_size: int = Field(..., description="每页大小", example=10)
    pages: int = Field(..., description="总页数", example=10)

class PaginatedWingknifeExecutions(PaginatedResult):
    """分页影刀执行记录结果"""
    items: List[WingknifeExecution] = Field(..., description="执行记录列表")

    class Config:
        json_schema_extra = {
            "example": {
                "total": 100,
                "page": 1,
                "page_size": 10,
                "pages": 10,
                "items": [
                    {
                        "id": 1,
                        "process_id": 1,
                        "run_id": "abc-123-xyz",
                        "status": "completed",
                        "parameters": {"date": "2023-01-01", "limit": 100},
                        "result": {"recordsProcessed": 120, "successCount": 118, "failureCount": 2},
                        "started_at": "2023-01-01T12:12:12",
                        "completed_at": "2023-01-01T12:35:46",
                        "created_at": "2023-01-01T12:12:12",
                        "created_by": "admin"
                    }
                ]
            }
        }

class WingknifeRunRequest(BaseModel):
    """运行影刀流程的请求模型"""
    parameters: Dict[str, Any] = Field(default_factory=dict, description="执行参数", 
        example={"date": "2023-01-01", "limit": 100, "options": {"enableLogging": True}},
        alias='params')

    class Config:
        json_schema_extra = {
            "example": {
                "params": {
                    "date": "2023-01-01", 
                    "limit": 100, 
                    "options": {"enableLogging": True}
                }
            }
        }
        populate_by_name = True 